package com.xoa.service.equipment.impl;


import com.xoa.dao.equipment.EquipTypeMapper;
import com.xoa.dao.equipment.EquipmentCapysMapper;
import com.xoa.dao.equipment.EquipmentMapper;
import com.xoa.dao.equipment.LimsEquipCapyTypeMapper;
import com.xoa.model.equipment.EquipmentCapys;
import com.xoa.service.equipment.EquipmentCapysService;
import com.xoa.util.LimsJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EquipmentCapysServiceImpl implements EquipmentCapysService {

    @Autowired
    private EquipmentCapysMapper equipmentCapysMapper;

    @Autowired
    private EquipTypeMapper equipTypeMapper;

    @Autowired
    private EquipmentMapper equipmentMapper;

    @Autowired
    private LimsEquipCapyTypeMapper limsEquipCapyTypeMapper;

    //@Autowired
    //LimsCommonUnitsMapper limsCommonUnitsMapper;

    @Override
    public LimsJson<EquipmentCapys> selectAllEquipmentCapys() {
        LimsJson limsJson = new LimsJson(1, "err");
        List<EquipmentCapys> equipmentCapys = equipmentCapysMapper.selectAll();
        if (equipmentCapys.size() > 0) {
            limsJson.setCode(0);
            limsJson.setMsg("成功查询到" + equipmentCapys.size() + "条数据");
            limsJson.setData(equipmentCapys);
        } else {
            limsJson.setMsg("查询失败");
        }
        return limsJson;
    }

    @Override
    public LimsJson<EquipmentCapys> selectById(Integer Id) {
        LimsJson limsJson = new LimsJson(1, "err");
        if (Id != null) {
            EquipmentCapys equipmentCapys = equipmentCapysMapper.selectByPrimaryKey(Id);
            if (equipmentCapys != null) {
                limsJson.setCode(0);
                limsJson.setMsg("查询成功");
                limsJson.setObject(equipmentCapys);
            } else {
                limsJson.setMsg("查询失败");
            }
        }
        return limsJson;
    }

    @Override
    public LimsJson<EquipmentCapys> selectByCapyId(Integer capyId) {
        LimsJson limsJson = new LimsJson(1, "err");
        if (capyId != null) {
            List<EquipmentCapys> equipmentCapys = equipmentCapysMapper.selectByCapyId(capyId);
            if (equipmentCapys.size() > 0) {
                limsJson.setCode(0);
                limsJson.setMsg("成功查询到" + equipmentCapys.size() + "条数据");
                limsJson.setData(equipmentCapys);
            } else {
                limsJson.setMsg("查询失败");
            }
        }
        return limsJson;
    }

    @Override
    public LimsJson<EquipmentCapys> selectByEquipId(Integer equipId,String typeCode) {
        LimsJson limsJson = new LimsJson(1, "err");
        /*Map map = new HashMap();
        map.put("equipId",equipId);
        map.put("typeCode",typeCode);
        if (equipId != null) {
            List<EquipmentCapys> equipmentCapys = equipmentCapysMapper.selectByEquipIdandtypeCode(map);
            String codes="";
            if (equipmentCapys.size() > 0) {
                for(EquipmentCapys e:equipmentCapys){
                   codes+=e.getTypeCode()+",";
                    String names="";
                    if(e.getUnitIds()!=null&&!e.getUnitIds().equals("")){
                        String[] id = e.getUnitIds().split(",");
                        for(int i=0;i<id.length;i++){
                            LimsCommonUnits limsCommonUnits = limsCommonUnitsMapper.selectByPrimaryKey(Integer.parseInt(id[i]));
                            if(limsCommonUnits!=null&&!limsCommonUnits.getUnitName().equals("")&&limsCommonUnits!=null){
                                names+=limsCommonUnits.getUnitName()+",";
                            }
                        }
                    }
                    e.setUnitName(names);
                }
                String[] str = codes.split(",");
                List list = new ArrayList();
                for(int i= 0;i<str.length;i++){//数组去重
                    if(!list.contains(str[i])){
                        list.add(str[i]);
                    }
                }
                Map map1 = new HashMap();
                map1.put("codes",list);
                List<LimsEquipCapyType> limsEquipCapyTypes = limsEquipCapyTypeMapper.typeInfo(map1);
                List lists=new ArrayList();
                lists.add(limsEquipCapyTypes);
                lists.add(equipmentCapys);
                limsJson.setCode(0);
                limsJson.setMsg("成功查询到" + equipmentCapys.size() + "条数据");
                limsJson.setData(lists);
            } else {
                limsJson.setMsg("查询失败");
            }
        }*/
        return limsJson;
    }

    @Override
    public LimsJson<EquipmentCapys> addEquipmentCapys(EquipmentCapys equipmentCapys) {
        LimsJson limsJson = new LimsJson(1, "err");
        if (equipmentCapys != null) {
            int i = equipmentCapysMapper.insertSelective(equipmentCapys);
            if (i > 0) {
                limsJson.setCode(0);
                limsJson.setMsg("成功插入" + i + "条数据");
                limsJson.setObject(equipmentCapys);
            } else {
                limsJson.setMsg("插入失败");
            }
        }
        return limsJson;
    }

    @Override
    public LimsJson<EquipmentCapys> editEquipmentCapys(EquipmentCapys equipmentCapys) {
        LimsJson limsJson = new LimsJson(1, "err");
        if (equipmentCapys != null) {
            int i = equipmentCapysMapper.updateByPrimaryKeySelective(equipmentCapys);
            if (i > 0) {
                limsJson.setCode(0);
                limsJson.setMsg("成功修改" + i + "条数据");
                limsJson.setObject(equipmentCapys);
            } else {
                limsJson.setMsg("修改失败");
            }
        }
        return limsJson;
    }

    @Override
    public LimsJson<EquipmentCapys> deleteEquipmentCapys(Integer id) {
        LimsJson limsJson = new LimsJson(1, "err");
        if (id != null) {
            int i = equipmentCapysMapper.deleteByPrimaryKey(id);
            if (i > 0) {
                limsJson.setCode(0);
                limsJson.setMsg("成功删除" + i + "条数据");

            } else {
                limsJson.setMsg("删除失败");
            }
        }
        return limsJson;
    }

    @Override
    public LimsJson<EquipmentCapys> deleteArrayEquipmentCapys(String ids) {
        LimsJson limsJson = new LimsJson(1, "err");
        String[] idArray = ids.split(",");
        for (int i = 0; i < idArray.length; i++) {
            Integer id = Integer.parseInt(idArray[i]);
            int mes = equipmentCapysMapper.deleteByPrimaryKey(id);
            if (mes > 0) {
                limsJson.setCode(0);
            }
        }
        return limsJson;
    }


}
